package org.dbi.mysql;

import org.dbi.logger.ErrorLogger;
import java.sql.*;

public class DatabaseManager {
	private static DatabaseManager myManager = null;
	
	private String user 	= "root";
	private String password = "";
	private String host 	= "jdbc:mysql://localhost:3306/";
	private String database = "wallytestdb";
	private String driver 	= "com.mysql.jdbc.Driver";

	private Connection con;
	
	private  ErrorLogger logger = ErrorLogger.getLogger(getClass().getName());

	public static void main(String[] args) {
		DatabaseManager dbm = DatabaseManager.getInstance();	
		System.out.println(dbm.getConnection() ==null);
	}
	
	 public static DatabaseManager getInstance(){
		if(myManager == null){
			myManager = new DatabaseManager();
		}
		return myManager;
	}
	
	private DatabaseManager(){
		
		try {
			logger.info("Bezig met verbinding te maken naar de database.");		
			Class.forName(driver).newInstance();
			logger.info("Bezig met opzetten van de database connectie");	
			con = DriverManager.getConnection (host+database,user,password);
			logger.info("Connectie maken geslaagd.");
			logger.info("Verbinding geslaagd.");
		} catch (ClassNotFoundException e) {
			logger.severe(e.getMessage());
		} catch (SQLException e) {
			logger.severe(e.getMessage());
		} catch (InstantiationException e) {
			logger.severe(e.getMessage());
		} catch (IllegalAccessException e) {
			logger.severe(e.getMessage());
		}
		
	}
	
	public Connection getConnection(){
		return con;
	}

}
